home *** CD-ROM | disk | FTP | other *** search
/ AMIGA-CD 2 / Amiga-CD - Volume 2.iso / ungepackte_daten / 1994 / 8 / 05 / term-4.0-source.lha / termScale.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-17  |  8.7 KB  |  459 lines

  1. /*
  2. **    termScale.c
  3. **
  4. **    Single scaled character output routines
  5. **
  6. **    Copyright © 1990-1994 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* Some static data required by the bitmap scaling routine. */
  13.  
  14. STATIC struct RastPort        *ScaleRPort;
  15. STATIC struct BitMap        *ScaleSrcBitMap,
  16.                 *ScaleDstBitMap;
  17. STATIC struct BitScaleArgs    *ScaleArgs;
  18.  
  19. STATIC WORD             ScaleCache    = -1,
  20.                  ScaleType    = 42,
  21.                  ScaleConfig    = 42,
  22.                  PlaneWidth,
  23.                  PlaneHeight;
  24.  
  25.     /* DeleteScale():
  26.      *
  27.      *    Frees all the data associated with font scaling.
  28.      */
  29.  
  30. VOID
  31. DeleteScale()
  32. {
  33.     WORD i;
  34.  
  35.     if(ScaleArgs)
  36.     {
  37.         FreeVecPooled(ScaleArgs);
  38.  
  39.         ScaleArgs = NULL;
  40.     }
  41.  
  42.     if(ScaleDstBitMap)
  43.     {
  44.         for(i = 0 ; i < ScaleDstBitMap -> Depth ; i++)
  45.         {
  46.             if(ScaleDstBitMap -> Planes[i])
  47.                 FreeRaster(ScaleDstBitMap -> Planes[i],PlaneWidth * 2,PlaneHeight * 2);
  48.         }
  49.  
  50.         FreeVecPooled(ScaleDstBitMap);
  51.  
  52.         ScaleDstBitMap = NULL;
  53.     }
  54.  
  55.     if(ScaleSrcBitMap)
  56.     {
  57.         for(i = 0 ; i < ScaleSrcBitMap -> Depth ; i++)
  58.         {
  59.             if(ScaleSrcBitMap -> Planes[i])
  60.                 FreeRaster(ScaleSrcBitMap -> Planes[i],PlaneWidth,PlaneHeight);
  61.         }
  62.  
  63.         FreeVecPooled(ScaleSrcBitMap);
  64.  
  65.         ScaleSrcBitMap = NULL;
  66.     }
  67.  
  68.     if(ScaleRPort)
  69.     {
  70.         FreeVecPooled(ScaleRPort);
  71.  
  72.         ScaleRPort = NULL;
  73.     }
  74. }
  75.  
  76.     /* CreateScale():
  77.      *
  78.      *    Sets up the data required for real-time font scaling
  79.      *    (bitmaps, rastports, etc.).
  80.      */
  81.  
  82. BYTE
  83. CreateScale()
  84. {
  85.         /* Create a RastPort to render into. */
  86.  
  87.     if(ScaleRPort = (struct RastPort *)AllocVecPooled(sizeof(struct RastPort),MEMF_ANY | MEMF_CLEAR))
  88.     {
  89.         WORD MaxWidth,i;
  90.  
  91.             /* Initialize it. */
  92.  
  93.         InitRastPort(ScaleRPort);
  94.  
  95.         if(GFX)
  96.             MaxWidth = GFX -> tf_XSize;
  97.         else
  98.             MaxWidth = 0;
  99.  
  100.         if(TextFontWidth > MaxWidth)
  101.             MaxWidth = TextFontWidth;
  102.  
  103.             /* Remember dimensions. */
  104.  
  105.         PlaneWidth    = MaxWidth;
  106.         PlaneHeight    = TextFontHeight;
  107.  
  108.             /* Create the bitmap to render into. */
  109.  
  110.         if(ScaleSrcBitMap = (struct BitMap *)AllocVecPooled(sizeof(struct BitMap),MEMF_ANY))
  111.         {
  112.                 /* Initialize the bitmap. */
  113.  
  114.             InitBitMap(ScaleSrcBitMap,Window -> WScreen -> RastPort . BitMap -> Depth,PlaneWidth,PlaneHeight);
  115.  
  116.                 /* Create the bitmap to place the scaled font data into. */
  117.  
  118.             if(ScaleDstBitMap = (struct BitMap *)AllocVecPooled(sizeof(struct BitMap),MEMF_ANY))
  119.             {
  120.                 BYTE AllFine = TRUE;
  121.  
  122.                 InitBitMap(ScaleDstBitMap,Window -> WScreen -> RastPort . BitMap -> Depth,PlaneWidth * 2,PlaneHeight * 2);
  123.  
  124.                     /* Allocate the necessary memory space. */
  125.  
  126.                 for(i = 0 ; AllFine && i < ScaleSrcBitMap -> Depth ; i++)
  127.                 {
  128.                     if(!(ScaleSrcBitMap -> Planes[i] = AllocRaster(PlaneWidth,PlaneHeight)))
  129.                         AllFine = FALSE;
  130.                 }
  131.  
  132.                 if(AllFine)
  133.                 {
  134.                         /* Initialize destination bitmap, it must be
  135.                          * large enough to hold four times the size
  136.                          * of the source data.
  137.                          */
  138.  
  139.                         /* Allocate space for the destination area. */
  140.  
  141.                     for(i = 0 ; AllFine && i < ScaleDstBitMap -> Depth ; i++)
  142.                     {
  143.                         if(!(ScaleDstBitMap -> Planes[i] = AllocRaster(PlaneWidth * 2,PlaneHeight * 2)))
  144.                             AllFine = FALSE;
  145.                     }
  146.  
  147.                     if(AllFine)
  148.                     {
  149.                             /* Put the source bitmap into the source RastPort. */
  150.  
  151.                         ScaleRPort -> BitMap = ScaleSrcBitMap;
  152.  
  153.                             /* Install the fonts. */
  154.  
  155.                         SetFont(ScaleRPort,CurrentFont);
  156.  
  157.                             /* Set the default rendering pens. */
  158.  
  159.                         SetAPen(ScaleRPort,1);
  160.                         SetBPen(ScaleRPort,0);
  161.  
  162.                             /* By default, overwrite data. */
  163.  
  164.                         SetDrMd(ScaleRPort,JAM2);
  165.  
  166.                             /* Allocate space for the bitmap scaling arguments. */
  167.  
  168.                         if(ScaleArgs = (struct BitScaleArgs *)AllocVecPooled(sizeof(struct BitScaleArgs),MEMF_ANY | MEMF_CLEAR))
  169.                         {
  170.                                 /* Initialize the structure. */
  171.  
  172.                             ScaleArgs -> bsa_SrcWidth    = TextFontWidth;
  173.                             ScaleArgs -> bsa_SrcHeight    = TextFontHeight;
  174.  
  175.                             ScaleArgs -> bsa_YSrcFactor    = 1;
  176.  
  177.                             ScaleArgs -> bsa_SrcBitMap    = ScaleSrcBitMap;
  178.                             ScaleArgs -> bsa_DestBitMap    = ScaleDstBitMap;
  179.  
  180.                             return(TRUE);
  181.                         }
  182.                     }
  183.                 }
  184.             }
  185.         }
  186.     }
  187.  
  188.     return(FALSE);
  189. }
  190.  
  191.     /* PrintScaled(UBYTE Char,UBYTE Scale):
  192.      *
  193.      *    This is the big one: since VT100 supports a number of
  194.      *    font sizes (double height, double width, 132 columns),
  195.      *    the approriate characters are scaled in real-time before
  196.      *    they are displayed.
  197.      */
  198.  
  199. VOID __regargs
  200. PrintScaled(STRPTR Buffer,LONG Size,UBYTE Scale)
  201. {
  202.     UWORD SrcY,DestX,DestY,SizeX,Baseline;
  203.  
  204.         /* Determine the scale of the destination character. */
  205.  
  206. /*    if(ScaleType != Scale || ScaleConfig != Config -> EmulationConfig -> FontScale)*/
  207.  
  208.     {
  209.         if(Config -> EmulationConfig -> FontScale == SCALE_HALF)
  210.         {
  211.                 /* Determine scale to be used. */
  212.  
  213.             switch(Scale)
  214.             {
  215.                     /* Half width. */
  216.  
  217.                 case SCALE_ATTR_NORMAL:
  218.  
  219.                     ScaleArgs -> bsa_XDestFactor    = 1;
  220.                     ScaleArgs -> bsa_YDestFactor    = 1;
  221.                     ScaleArgs -> bsa_XSrcFactor    = 2;
  222.  
  223.                     SrcY    = 0;
  224.                     DestX    = MUL_X(CursorX) / 2;
  225.                     SizeX    = TextFontWidth / 2;
  226.  
  227.                     ScaleCache = -1;
  228.  
  229.                     break;
  230.  
  231.                     /* Half width, double height (top bits). */
  232.  
  233.                 case SCALE_ATTR_TOP2X:
  234.  
  235.                     ScaleArgs -> bsa_XDestFactor    = 1;
  236.                     ScaleArgs -> bsa_YDestFactor    = 2;
  237.                     ScaleArgs -> bsa_XSrcFactor    = 1;
  238.  
  239.                     SrcY    = 0;
  240.                     DestX    = MUL_X(CursorX);
  241.                     SizeX    = TextFontWidth;
  242.  
  243.                     ScaleCache = -1;
  244.  
  245.                     break;
  246.  
  247.                     /* Half width, double height (bottom bits). */
  248.  
  249.                 case SCALE_ATTR_BOT2X:
  250.  
  251.                     ScaleArgs -> bsa_XDestFactor    = 1;
  252.                     ScaleArgs -> bsa_YDestFactor    = 2;
  253.                     ScaleArgs -> bsa_XSrcFactor    = 1;
  254.  
  255.                     SrcY    = TextFontHeight;
  256.                     DestX    = MUL_X(CursorX);
  257.                     SizeX    = TextFontWidth;
  258.  
  259.                     ScaleCache = -1;
  260.  
  261.                     break;
  262.             }
  263.         }
  264.         else
  265.         {
  266.                 /* Determine scale to be used. */
  267.  
  268.             switch(Scale)
  269.             {
  270.                     /* Double height (top bits). */
  271.  
  272.                 case SCALE_ATTR_TOP2X:
  273.  
  274.                     ScaleArgs -> bsa_XDestFactor    = 2;
  275.                     ScaleArgs -> bsa_YDestFactor    = 2;
  276.                     ScaleArgs -> bsa_XSrcFactor    = 1;
  277.  
  278.                     SrcY    = 0;
  279.                     DestX    = MUL_X(CursorX) * 2;
  280.                     SizeX    = TextFontWidth * 2;
  281.  
  282.                     ScaleCache = -1;
  283.  
  284.                     break;
  285.  
  286.                     /* Double height (bottom bits). */
  287.  
  288.                 case SCALE_ATTR_BOT2X:
  289.  
  290.                     ScaleArgs -> bsa_XDestFactor    = 2;
  291.                     ScaleArgs -> bsa_YDestFactor    = 2;
  292.                     ScaleArgs -> bsa_XSrcFactor    = 1;
  293.  
  294.                     SrcY    = TextFontHeight;
  295.                     DestX    = MUL_X(CursorX) * 2;
  296.                     SizeX    = TextFontWidth * 2;
  297.  
  298.                     ScaleCache = -1;
  299.  
  300.                     break;
  301.  
  302.                     /* Double width. */
  303.  
  304.                 case SCALE_ATTR_2X:
  305.  
  306.                     ScaleArgs -> bsa_XDestFactor    = 2;
  307.                     ScaleArgs -> bsa_YDestFactor    = 1;
  308.                     ScaleArgs -> bsa_XSrcFactor    = 1;
  309.  
  310.                     SrcY    = 0;
  311.                     DestX    = MUL_X(CursorX) * 2;
  312.                     SizeX    = TextFontWidth * 2;
  313.  
  314.                     ScaleCache = -1;
  315.  
  316.                     break;
  317.             }
  318.         }
  319.  
  320.         ScaleType    = Scale;
  321.         ScaleConfig    = Config -> EmulationConfig -> FontScale;
  322.     }
  323.  
  324.         /* Look for the font type to scale. */
  325.  
  326.     if(ScaleRPort -> Font != CurrentFont)
  327.     {
  328.         SetFont(ScaleRPort,CurrentFont);
  329.  
  330.         ScaleArgs -> bsa_SrcWidth = TextFontWidth;
  331.  
  332.         ScaleCache = -1;
  333.     }
  334.  
  335.         /* Set the approriate colours. */
  336.  
  337.     if(ReadAPen(ScaleRPort) != ReadAPen(RPort))
  338.     {
  339.         SetAPen(ScaleRPort,ReadAPen(RPort));
  340.  
  341.         ScaleCache = -1;
  342.     }
  343.  
  344.     if(ReadBPen(ScaleRPort) != ReadBPen(RPort))
  345.     {
  346.         SetBPen(ScaleRPort,ReadBPen(RPort));
  347.  
  348.         ScaleCache = -1;
  349.     }
  350.  
  351.         /* Calculate topmost line to write to. */
  352.  
  353.     DestY = MUL_Y(CursorY);
  354.  
  355.         /* Remember the font baseline. */
  356.  
  357.     Baseline = CurrentFont -> tf_Baseline;
  358.  
  359.     if(CurrentFont == GFX)
  360.     {
  361.         BYTE Mode = 1;
  362.  
  363.             /* Run down the buffer... */
  364.  
  365.         while(Size--)
  366.         {
  367.             if(GfxTable[*Buffer] == Mode)
  368.             {
  369.                 if(*Buffer != ScaleCache)
  370.                 {
  371.                     ScaleCache = *Buffer;
  372.  
  373.                         /* Print the character to be scaled into the
  374.                          * invisible drawing area.
  375.                          */
  376.  
  377.                     Move(ScaleRPort,0,Baseline);
  378.  
  379.                     Text(ScaleRPort,Buffer++,1);
  380.  
  381.                         /* Scale the font. */
  382.  
  383.                     BitMapScale(ScaleArgs);
  384.                 }
  385.                 else
  386.                     Buffer++;
  387.  
  388.                     /* Render the character. */
  389.  
  390.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  391.             }
  392.             else
  393.             {
  394.                 ScaleCache = *Buffer;
  395.  
  396.                 if(Mode)
  397.                     SetFont(ScaleRPort,TextFont);
  398.                 else
  399.                     SetFont(ScaleRPort,GFX);
  400.  
  401.                 Mode ^= 1;
  402.  
  403.                     /* Print the character to be scaled into the
  404.                      * invisible drawing area.
  405.                      */
  406.  
  407.                 Move(ScaleRPort,0,Baseline);
  408.  
  409.                 Text(ScaleRPort,Buffer++,1);
  410.  
  411.                     /* Scale the font. */
  412.  
  413.                 BitMapScale(ScaleArgs);
  414.  
  415.                     /* Render the character. */
  416.  
  417.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  418.             }
  419.  
  420.             DestX += SizeX;
  421.         }
  422.  
  423.         if(!Mode)
  424.             SetFont(ScaleRPort,GFX);
  425.     }
  426.     else
  427.     {
  428.             /* Run down the buffer... */
  429.  
  430.         while(Size--)
  431.         {
  432.             if(*Buffer != ScaleCache)
  433.             {
  434.                 ScaleCache = *Buffer;
  435.  
  436.                     /* Print the character to be scaled into the
  437.                      * invisible drawing area.
  438.                      */
  439.  
  440.                 Move(ScaleRPort,0,Baseline);
  441.  
  442.                 Text(ScaleRPort,Buffer++,1);
  443.  
  444.                     /* Scale the font. */
  445.  
  446.                 BitMapScale(ScaleArgs);
  447.             }
  448.             else
  449.                 Buffer++;
  450.  
  451.                 /* Render the character. */
  452.  
  453.             BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,WindowLeft + DestX,WindowTop + DestY,SizeX,TextFontHeight,MINTERM_COPY);
  454.  
  455.             DestX += SizeX;
  456.         }
  457.     }
  458. }
  459.